<?php
namespace app\admin\controller;

class Xydzp extends Common
{
    private $obj;
    public function _initialize()
    {
        parent::_initialize();
        $this->obj = model('Xydzp');
    }

    public function index()
    {
        $where = [];
        $data = $this->obj->order('id desc')->where($where)->select();
        $this->assign('data', $data);
        return view();
    }

    public function xydzp_save() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['name'] = input('post.name', '');
            $data['img'] = input('post.img', '');
            $data['status'] = input('post.status', 0);
            $data['greeting'] = input('post.greeting', '');
            $data['rule'] = input('post.rule', '');

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = $this->obj->save($data, $where);

            // 清理缓存
            \app\api\service\Base::clear_cache();

            if ($rs) {
                $this->success('保存成功', '');
            } else {
                $this->error('保存失败');
            }
        } else{
            $data = [];
            if ($id > 0) {
                $data = $this->obj->get(['id' => $id]);
            }
            $this->assign('data', $data);
            return view();
        }
    }

    public function xydzp_prize_index()
    {
        $where = [];
        $where['xydzp_id'] = input('param.id');
        $data = model('xydzp_prize')->order('id desc')->where($where)->select();
        $this->assign('data', $data);
        $this->assign('xydzp_id', input('param.id'));
        return view();
    }

    public function xydzp_prize_save() {
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['xydzp_id'] = input('post.xydzp_id', 0);
            $data['name'] = input('post.name', '');
            $data['num'] = input('post.num', 0);
            $data['img'] = input('post.img', '');
            $data['probability'] = input('post.probability', 0);
            $data['status'] = input('post.status', 0);

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = model('xydzp_prize')->save($data, $where);

            // 清理缓存
            \app\api\service\Base::clear_cache();

            if ($rs) {
                $this->success('保存成功', '');
            } else {
                $this->error('保存失败');
            }
        } else{
            $xydzp_id = input('param.xydzp_id', 0);
            $data = [];
            if ($id > 0) {
                $data = model('xydzp_prize')->get(['id' => $id]);
                $xydzp_id = $data->xydzp_id;
            }
            $this->assign('data', $data);
            $this->assign('xydzp_id', $xydzp_id);
            return view();
        }
    }
}
